package org.edu.modules.oa.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.formula.functions.T;
import org.edu.modules.oa.config.HttpURLConnectionUtil;
import org.edu.modules.oa.entity.DepartmentEntity;
import org.edu.modules.oa.entity.VeOaNoticeEntity;
import org.edu.modules.oa.entity.bo.BasicResponseBO;
import org.edu.modules.oa.entity.bo.DepartmentBo;
import org.edu.modules.oa.mapper.DepartmentMapper;
import org.edu.modules.oa.mapper.IVeOaNoticeMapper;
import org.edu.modules.oa.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author 林彬辉
 * @version V1.0
 * @Package org.edu.modules.oa.service.impl
 * @date 2021/7/31 14:06
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentEntity> implements DepartmentService {

    @Value("${common.host}")
    private String dirHost;

    private String interfaceUserId = "?" +
            "interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";

    @Autowired
    ObjectMapper mapper;

    @Resource
    private DepartmentMapper departmentMapper;
    //查所有部门信息
    @Override
    public BasicResponseBO<List<DepartmentBo>> getDepartmentAll() {
//        dirHost = "http://39.108.144.105:6009/";
        String url = "/common/veCommon/queryDepartmentList" + interfaceUserId;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<DepartmentBo>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()), DepartmentBo.class)) ;
        return responseBO;

    }



    /**
     * 调用公告数据接口
     */
    private <T> BasicResponseBO<T> getUrl(String url, Class<T> clazz) {
        try {
            url = dirHost+url;
            String result = HttpURLConnectionUtil.doGet(url) ;
            BasicResponseBO<T> re = mapper.readValue(result, new TypeReference<BasicResponseBO<T>>() {});
            T body = mapper.readValue(mapper.writeValueAsString(re.getResult()), clazz);
             re.setResult(body);
            return re;
        } catch (Exception e) {
            return new BasicResponseBO<T>().setSuccess(false).setMessage("操作失败");
        }
    }


}
